﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using POSSystem.BLL;

namespace ShopOnline.Controls
{
    public partial class ProductListCtrl : BaseDataListCtrl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), ClientID, "if(window['addDivider'] != undefined)addDivider();", true);
            index = 0;
        }

        public event CartChangedEventHandler CartChanged;
        private void OnCartChanged(EventArgs e)
        {
            if (CartChanged != null)
                CartChanged(this, e);
        }

        protected int index = 0;

        protected void btnAddWishList_OnClick(object sender, EventArgs e)
        {
            if (View_Customers.GetCurrentUser() != null && View_Customers.GetCurrentUser().RowCount > 0)
            {
                int id = int.Parse(hidID.Value);
                POSSystem.BLL.WishList w = POSSystem.BLL.WishList.GetByCustomer(View_Customers.GetCurrentUser().ID, id);
                if (w.RowCount > 0)
                {
                    ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'This item has already been added to your WishList!', 8000);", 1), true);
                    return;
                }
                w.AddNew();
                w.CustomerID = View_Customers.GetCurrentUser().ID;
                w.ProductID = id;
                w.CreatedDate = DateTime.UtcNow.AddHours(SystemVariables.SysVars.TimeZone);
                w.Deleted = false;
                w.Save();

                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Your WishList has been updated!', 8000);", 1), true);
            }
            else
                ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showErrorAlert('All For You Online', 'You have to login first!', 8000);", 1), true);
        }

        protected void btnAddCart_OnClick(object sender, EventArgs e)
        {
            int id = int.Parse(hidID.Value);

            View_WebProducts p = View_WebProducts.LoadProductByID(id);

            OrderItems items = BasePage.SelectedItems;
            items.AddNew();
            if (!items.DefaultView.Table.Columns.Contains(BasePage.PRODUCT_IMAGE))
                items.AddColumn(BasePage.PRODUCT_IMAGE, typeof(string));
            items.ProductID = id;
            items.ID = -items.RowCount;
            items.ProductName = p.s_ProductName;
            items.Quantity = 1;
            items.s_CustomerPrice = (SessionUser != null && SessionUser.Price == CustomerType.CustomerPrice.DealerPrice) ? p.s_DealerPrice : ((SessionUser != null && SessionUser.Price == CustomerType.CustomerPrice.PartnerPrice) ? p.s_PartnerPrice : p.s_SalePrice);
            items.s_UnitCost = p.s_Cost;
            items.s_UnitPrice = p.s_SalePrice;
            items.s_UnitDealerPrice = p.s_DealerPrice;
            items.TotalAmount = items.Quantity * items.CustomerPrice;
            items.GST = p.GST ? Math.Round(items.TotalAmount * SysVars.GST / (SysVars.GST + 100), 2) : 0;
            items.Deleted = false;
            items.BarCode = p.s_BarCode;
            items.s_ProductImage = p.s_ProductImageID;
            items.Category = OrderItemCategory.Product;
            items.Name = p.ProductName;
            if (items.s_ProductImage != "")
            {
                ProductImages img = ProductImages.GetById(int.Parse(items.s_ProductImage));
                if (img.RowCount > 0)
                    items.SetColumn(BasePage.PRODUCT_IMAGE, ShopOnline.Utilities.GetImageUrl(img.s_ThumbUrl));
            }
            else
                items.SetColumn(BasePage.PRODUCT_IMAGE, GetDefaultPic());

            OnCartChanged(e);
            ScriptManager.RegisterStartupScript(this, typeof(Page), Guid.NewGuid().ToString(), String.Format("showNotification('All For You Online', 'Your cart has been updated!', 8000);", 1), true);
        }
    }
}